package ar.edu.unicen.exa.isistan.archassis.ia.api.impl;

import java.util.ArrayList;
import java.util.List;

import ar.edu.unicen.exa.isistan.archassis.ia.api.ISummaryData;
import ar.edu.unicen.exa.isistan.archassis.ia.api.db.JavaClass;
import ar.edu.unicen.exa.isistan.archassis.ia.seed.ImplementsSeed;
import ar.edu.unicen.exa.isistan.archassis.ia.seed.MetodoSeed;
import ar.edu.unicen.exa.isistan.archassis.ia.seed.Run;
import ar.edu.unicen.exa.isistan.archassis.ia.seed.VariableSeed;

public abstract class AbstractSummaryData implements ISummaryData {

	private JavaClass resultClass; // puede ser null
	private ArrayList<String> listImplementsClass;
	private ArrayList<Run> listRunClass;
	private ArrayList<VariableSeed> listVarClass;
	private boolean ordenado = false;
	
	private static int automaticName = 0;
	private static synchronized int getNewId() {
		return automaticName++;
	}
	private String name = null;

	@Override
	public String getName() {
		if (name == null) {
			name = "Result" + getNewId();
		}
		return name;
	}
	
	public AbstractSummaryData(JavaClass clase) {
		this.resultClass = clase;
	}

	/**
	 * @return the javaClass
	 */
	public JavaClass getJavaClass() {
		return resultClass;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		String javaClassText = getClassName();
		return "AISeedResult [valor=" + 5000 + "|javaClass=" + javaClassText
				+ "]";
	}

	@Override
	public String getExtendsClass() {
		if (this.resultClass == null) {
			return "NULL_SEED";
		}
		return this.resultClass.getEntiende();
	}

	@Override
	public String getClassName() {
		if (resultClass == null) {
			return "NULL_SEED";
		}
		return resultClass.getNombre();
	}
	


	@Override
	public List<String> getImplementsClass() {
		if (listImplementsClass == null) {
			listImplementsClass = new ArrayList<String>();
			if (resultClass != null) {
				for (ImplementsSeed implClass : this.resultClass
						.getImplementa()) {
					listImplementsClass.add(implClass.getValor());
				}
			}
		}
		return listImplementsClass;
	}

	@Override
	public List<Run> getRunClass() {
		if (listRunClass == null) {
			listRunClass = new ArrayList<Run>();
			if (resultClass != null) {
				for (MetodoSeed metClass : this.resultClass.getMetodos()) {
					for (Run run : metClass.getEjecuta()) {
						listRunClass.add(run);
					}
				}
			}
		}
		return listRunClass;
	}
	
	public List<MetodoSeed> getListMetodo(){
		return this.resultClass.getMetodos();
	}

	@Override
	public List<VariableSeed> getVarClass() {
		if (listVarClass == null) {
			listVarClass = new ArrayList<VariableSeed>();
			if (resultClass != null) {
				for (VariableSeed varClass : this.resultClass.getVariables()) {
					listVarClass.add(varClass);
				}
			}
		}
		return listVarClass;
	}

}
